/**
 * Created by IntelliJ IDEA.
 * Time: 17:15
 * @Author: xyb
 * @Version: 2017/7/6
 * To change this template use File | Settings | File Templates.
 */

(function () {

    var $view;

    var $form;
    var $account;
    var $pass;
    var $submit;
    var $input;


    function onInit() {
        onInitData();

        monitorEdit();

        clearInputText();

        submitLogin();
    }

    /**
     * 初始化数据dom
     */
    function onInitData() {
        $form = $view.find('#form');
        $account = $form.find(".account");
        $pass = $form.find(".pass");
        $submit = $form.find(".submit");
        $input = $form.find('.input');
    }

    /**
     * 监听input
     */
    function monitorEdit() {
        $input.bind("input propertychange", function (data) {
            // console.log(data.target.value)
            var $this = $(this);
            var value = data.target.value;
            if (value.length > 0) {
                $this.parent().addClass("icon");
            } else {
                $this.parent().removeClass("icon");
            }
            if ($account.val() != "" && $pass.val() != "") {
                $submit.addClass("gold");
            } else {
                $submit.removeClass("gold");
            }
        });

        /**
         * 帐号输入框失去焦点
         */
        $account.blur(function () {
            var $this = $(this);
            var value = $this.val().trim();
            if (value != null && value != "") {
                if (value.indexOf("@") != -1) {
                    var boolean = app.validateEmail(value);
                    if (!boolean) {
                        app.showToast("请输入正确的邮箱!");
                        focusAccount();
                        return false;
                    }
                } else if (value.indexOf("1", 0) != -1) {
                    var boolean = app.validateMobileNum(value);
                    if (!boolean) {
                        app.showToast("请输入正确的手机号!");
                        focusAccount();
                        return false;
                    }
                } else {
                    focusAccount();
                    app.showToast("请输入正确的账号!")
                }
            }
        });

        /**
         * 帐号输入框获取焦点
         */
        function focusAccount() {
            setTimeout(function () {
                $account.focus();
            }, 1000);
        }
    }


    /**
     * 清空input中的字符
     */
    function clearInputText() {
        $input.next().on("click", function () {
            $(this).prev().val("");
            $(this).parent().removeClass("icon");
            $submit.removeClass("gold");
        });
    }

    /**
     * 登录
     */
    function submitLogin() {
        $form.on("click", '.submit.gold', function () {
            var data = {
                acc: $account.val(),
                pass: $pass.val()
            };
            backService.user.onLogin(data, function (json) {
                app.showToast(json.msg);
                if (json.data != null) {
                    app.gotoPage("/#");
                    localStorage.loginInfo = JSON.stringify(json.data);
                    backService.setUserLoginInfo(localStorage.loginInfo);
                }
            }, function (error) {
                app.showToast(error.msg);
            });
        });
    }


    window.onViewInit_user_login = function (view) {
        $view = view.element;
        onInit();
    }
})();